package com.authine.cloudpivot.ext.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @ClassName GatewayResponse
 * @Description: TODO
 * @Author fj
 * @Date 2021/5/14
 * @Version V1.0
 **/
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GatewayResponse<T> implements Serializable {

    private static final long serialVersionUID = -6030774809758157817L;

    public static final int RESPONSE_OK = 0;    // 成功：零和正整数
    public static final int RESPONSE_ERR = -1;    // 失败：负整数

    private int count;// 返回结果条数

    private int code;// 错误码，成功返回0

    private String msg;// 错误消息

    private T data;// 返回结果

    GatewayResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // success
    public static GatewayResponse success() {

        return new GatewayResponse(RESPONSE_OK, "", null);
    }

    public static GatewayResponse success(String msg) {

        return new GatewayResponse(RESPONSE_OK, msg, null);
    }

    public static <S> GatewayResponse<S> success(String msg, S data) {

        return new GatewayResponse<>(RESPONSE_OK, msg, data);
    }

    public static <S> GatewayResponse<S> success(int count, S data) {

        return new GatewayResponse<>(count, RESPONSE_OK, "", data);
    }

    public static <S> GatewayResponse<S> success(int count, String msg, S data) {

        return new GatewayResponse<>(count, RESPONSE_OK, msg, data);
    }

    // failed
    public static  GatewayResponse failed() {

        return new GatewayResponse(RESPONSE_ERR, "", null);
    }

    public static  GatewayResponse failed(String msg) {

        return new GatewayResponse(RESPONSE_ERR, msg, null);
    }

    public static <S> GatewayResponse<S> failed(String msg, S data) {

        return new GatewayResponse<>(RESPONSE_ERR, msg, data);
    }

    public static <S> GatewayResponse<S> failed(int count, S data) {

        return new GatewayResponse<>(RESPONSE_ERR, count, "", data);
    }

    public static <S> GatewayResponse<S> failed(int count, String msg, S data) {

        return new GatewayResponse<>(RESPONSE_ERR, count, msg, data);
    }

    public boolean isSuccess(){
        if (this.code == 0){
            return true;
        }
        return false;
    }
}
